package com.rongshu.sendmessage.fragment.util;

import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.rongshu.sendmessage.R;


/**
 * Created by Administrator on 2018/4/26.
 */

public class FrgManager {

    private FragmentActivity mContext;
    FragmentManager fm;

    public FrgManager(FragmentActivity mContext){
        this.mContext = mContext;
        this.fm = mContext.getSupportFragmentManager();
    }
    public static interface OnItemViewClickedListener {
        public void onClicked(View view);
    }

    public void replace(Fragment frg, int position){
        FragmentTransaction fragmentTransaction = mContext.getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.childFragmentContainer, frg, "tab_select");
        fragmentTransaction.commit();
    }


    public void add(Fragment frg, String tag){
        FragmentTransaction trans = fm.beginTransaction();
        trans.replace(R.id.childFragmentContainer,frg);
        trans.addToBackStack(tag);
        trans.commit();
    }

    public void pop(String tag, int flags){
        fm.popBackStackImmediate(tag,flags);
    }

    /**
     * 弹出回退栈中的所有fragment
     */
    public void popAllFrg(){
        pop(null,1);
    }

    /**
     * 弹出回退栈中最上层的那个fragment
     */
    public void popTopFrg(){
        pop(null,0);
    }

    /**
     * flags为0时，弹出该
     * fragment以上的Fragment
     * @param tag
     */
    public void popFrgExcludeSelf(String tag){
        pop(tag,0);
    }

    /**
     * 如果是1，弹出该fragment（包括该fragment）以上的fragment
     * @param tag
     */
    public void popFrgIncludeSelf(String tag){
        pop(tag,1);
    }

    public FragmentManager getFManager(){
        return fm;
    }
}
